package com.yyh.BookLendSystem.ui;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;

import com.yyh.BookLendSystem.dao.BookDao;
import com.yyh.BookLendSystem.util.Utils;

import swing2swt.layout.BorderLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class BookMgrCmp extends Composite {
	private Text text;
	private Text text_1;
	private Table table;
	private Combo combo;
	private BookDao bookDao =new BookDao();
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public BookMgrCmp(Composite parent, int style) {
		super(parent, SWT.NONE);
		setLayout(new BorderLayout(0, 0));
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayoutData(BorderLayout.NORTH);
		RowLayout rl_composite = new RowLayout(SWT.HORIZONTAL);
		rl_composite.marginLeft = 5;
		rl_composite.marginHeight = 5;
		rl_composite.center = true;
		composite.setLayout(rl_composite);
		
		Label label = new Label(composite, SWT.NONE);
		label.setText("书名");
		
		text = new Text(composite, SWT.BORDER);
		
		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setText("作者");
		
		text_1 = new Text(composite, SWT.BORDER);
		
		Label label_1 = new Label(composite, SWT.NONE);
		label_1.setText("价格");
		
		combo = new Combo(composite, SWT.NONE);
		combo.setItems(new String[] {"0~50", "51~100", "100~200", "..."});
		
		Button button = new Button(composite, SWT.NONE);
		
		button.setText("查询");
		
		Button button_1 = new Button(composite, SWT.NONE);
		
		button_1.setText("新增");
		
		Button button_2 = new Button(composite, SWT.NONE);
		
		button_2.setText("修改");
		
		Button button_3 = new Button(composite, SWT.NONE);
		button_3.setText("删除");
		
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLayoutData(BorderLayout.CENTER);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("编号");
		
		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(100);
		tableColumn_1.setText("书名");
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(100);
		tblclmnNewColumn.setText("作者");
		
		TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
		tableColumn_2.setWidth(100);
		tableColumn_2.setText("分类");
		
		TableColumn tableColumn_3 = new TableColumn(table, SWT.NONE);
		tableColumn_3.setWidth(100);
		tableColumn_3.setText("出版日期");
		
		TableColumn tableColumn4 = new TableColumn(table, SWT.NONE);
		tableColumn4.setWidth(100);
		tableColumn4.setText("买入时间");
		
		TableColumn tableColumn5 = new TableColumn(table, SWT.NONE);
		tableColumn5.setWidth(100);
		tableColumn5.setText("价格");
		query();
		
		//////////////////////////
		/**
		 * 查询
		 */
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				query();
			}
		});
		/*
		 * 新增
		 */
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				BookEditDlg bed = new BookEditDlg(BookMgrCmp.this.getShell(),SWT.NONE);
				bed.open();
			}
		});
		
		/**
		 * 修改
		 */
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//获取用户选择的图书
				TableItem[] items = table.getSelection();
				if(items ==null || items.length==0) {
					Utils.showMessage(getShell(), "请选择要修改的图书");
					return;
				}
				
				Object res = new BookEditDlg(getShell(), SWT.NONE,items[0]).open();
				if(res ==null ) {
					return;
				}
				if((boolean)res ==true) {
					query();
				}
				
				
			}
		});

	}
	
	public void query() {
		String name = text.getText().trim();
		String author = text_1.getText();
		String prices = combo.getText();
		List<Map<String,Object>>  list  = bookDao.select(name, author, prices);
		
		//清空表格
		table.removeAll();
		//渲染数据
		list.forEach(book->{
			TableItem tableItem = new TableItem(table,SWT.NONE);
			Collection<Object> values = book.values();
			//创建List集合，并将值传入
			//List<Object> row =new ArrayList<>(values);
			String[] svalues = values.stream().map(o->o==null?"":o.toString()).toArray(String[]::new);
			
		
//			for (int i = 0; i < svalues.length; i++) {
//				if(row.get(i)==null) {
//					svalues[i]= "";
//				}else {
//					svalues[i] = row.get(i).toString();
//				}	
//			}
			tableItem.setText(svalues);
			
		}); 
	}
	
	
	

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
